package com.jiwei.community.controller;

import com.jiwei.community.entity.Collection;
import com.jiwei.community.entity.Post;
import com.jiwei.community.enums.AppHttpCodeEnum;
import com.jiwei.community.exception.SystemException;
import com.jiwei.community.service.CollectionService;
import com.jiwei.community.service.PostService;
import com.jiwei.community.utils.SecurityUtils;
import com.jiwei.community.vo.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 季伟
 * @date 2024/4/1
 */
@RestController
@RequestMapping("/collection")
public class CollectionController {
    @Resource
    CollectionService collectionService;
    @PostMapping("")
    public ResponseResult collectPost(Long postId){
        if (postId == null)return ResponseResult.errorResult(AppHttpCodeEnum.BAD_REQUEST,"postId不能为空");
        return collectionService.collectPost(postId,SecurityUtils.getUserId());
    }
    @DeleteMapping("/cancel")
    public ResponseResult cancelCollect(Long postId){
        if (postId == null)return ResponseResult.errorResult(AppHttpCodeEnum.BAD_REQUEST,"postId不能为空");
        return collectionService.cancelCollect(postId,SecurityUtils.getUserId());
    }
    @GetMapping("/list")
    public ResponseResult getCollectionList(Integer pageNum,Integer pageSize){
        return collectionService.getCollectionList(pageNum,pageSize, SecurityUtils.getUserId());

    }
    @GetMapping("/isCollected")
    public ResponseResult isCollected(Long postId){
        if (postId == null)return ResponseResult.errorResult(AppHttpCodeEnum.BAD_REQUEST,"postId不能为空");
        try{
            Long userId = SecurityUtils.getUserId();
            if (userId != null){
                return collectionService.isCollected(userId,postId);
            }
        }catch (SystemException e){}
        Map map = new HashMap();
        map.put("isCollected",false);
        return ResponseResult.okResult(map);
    }

}
